package com.acegear.horizon.domain.models.jpa.convert;

import java.io.IOException;
import java.util.List;

import javax.persistence.AttributeConverter;

import com.acegear.horizon.domain.models.jpa.SignupItem;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import javaslang.jackson.datatype.JavaslangModule;

public class SignupItemConverter implements AttributeConverter<List<SignupItem>, String> {

	private ObjectMapper mapper;

	public SignupItemConverter() {
		mapper = new ObjectMapper();
		mapper.registerModule(new JavaslangModule());
	}

	@Override
	public String convertToDatabaseColumn(List<SignupItem> attribute) {
		if (attribute == null)
			return null;
		try {
			return mapper.writeValueAsString(attribute);
		} catch (JsonProcessingException e) {
			e.printStackTrace();

		}
		return null;
	}

	@Override
	public List<SignupItem> convertToEntityAttribute(String dbData) {
		if (dbData == null)
			return null;
		try {
			return mapper.readValue(dbData, new TypeReference<List<Long>>() {
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
